﻿(function () {
    $(function () {
        var _$incomeexpendTable = $('#IETypesTable');
        var _incomeexpendService = abp.services.app.dataMaintain; //对应application中接口前缀
        //对应AppPermissions里面的Pages_FinanceModule_DataMaintains等变量
        var _permissions = {
            create: abp.auth.hasPermission('Pages.FinanceModule.DataMaintains.CreateIEType'),
            edit: abp.auth.hasPermission('Pages.FinanceModule.DataMaintains.EditIEType'),
            'delete': abp.auth.hasPermission('Pages.FinanceModule.DataMaintains.DeleteIEType')
        };
        //新增/编辑账户窗体定义
        var _createOrEditIETypeModal = new app.ModalManager({
            viewUrl: abp.appPath + 'FinanceModule/DataMaintains/CreateOrEditIncomeExpendTypeModal',
            scriptUrl: abp.appPath + 'Areas/FinanceModule/Views/DataMaintains/_CreateOrEditIncomeExpendTypeModal.js',
            modalClass: 'CreateOrEditIncomeExpendTypeModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });
        $("#TabIncomeExpendTypes").click(function () {
           
            //表格定义，包括样式，触发事件等。
            _$incomeexpendTable.jtable({

                //title: app.localize('Pages_BasicDataModule_Classrooms'),
                paging: true,
                sorting: true,
                multiSorting: true,

                actions: {
                    listAction: {
                        method: _incomeexpendService.getIncomeExpendTypes
                    }
                },

                fields: {
                    id: {
                        key: true,
                        list: false
                    },
                    actions: {
                        title: app.localize('Actions'),
                        width: '20%',
                        sorting: false,
                        list: _permissions.edit || _permissions.delete,
                        display: function (data) {
                            var $span = $('<span></span>');

                            if (_permissions.edit) {
                                $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                    .appendTo($span)
                                    .click(function () {
                                        _createOrEditIETypeModal.open({ id: data.record.id });
                                    });
                            }

                            if (_permissions.delete) {
                                $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                    .appendTo($span)
                                    .click(function () {
                                        deleteIncomeExpendType(data.record);
                                    });
                            }

                            return $span;
                        }
                    },
                    //网格里面字段必须驼峰写法
                    ieTypeName: {
                        title: app.localize('Pages_FinanceModule_IETypes_GridColumn_IEName'),
                        width: '20%'
                    },
                    ieTypeId: {
                        title: app.localize('Pages_FinanceModule_IETypes_GridColumn_IEtype'),
                        width: '10%',
                        display: function (data) {
                            if (data.record.ieTypeId == 1) {
                                return app.localize('Pages_FinanceModule_IEType1');
                            } else {
                                return '<span style="color: rgb(253, 140, 6);">' + app.localize('Pages_FinanceModule_IEType2') + '</span>';
                                
                            }
                        }
                    },
                    
                    remark: {
                        title: app.localize('Pages_FinanceModule_IETypes_GridColumn_Remark'),
                        width: '50%'
                    }
                }

            });//表格定义结束

            //加载数据方法
            function getIncomeExpendTypes(reload) {
                if (reload) {
                    _$incomeexpendTable.jtable('reload');
                } else {
                    _$incomeexpendTable.jtable('load', {
                        filter: $('#IETypesTableFilter').val()
                    });
                }
            }
            //删除收支分类信息
            function deleteIncomeExpendType(o) {
                abp.message.confirm(
                    app.localize('Com_Msg_Delete_Warning', o.ieTypeName),
                    function (isConfirmed) {
                        if (isConfirmed) {
                            _incomeexpendService.deleteIncomeExpendType({
                                id: o.id
                            }).done(function () {
                                getIncomeExpendTypes(true);
                                abp.notify.success(app.localize('SuccessfullyDeleted'));
                            });
                        }
                    }
                );
            }
            //多条件查询框按钮
            $('#GetIETypesButton').click(function (e) {
                e.preventDefault();
                getIncomeExpendTypes();
            });
            //打开新增/编辑页面
            $('#CreateNewIETypeButton').click(function (e) {
                e.preventDefault();
                _createOrEditIETypeModal.open();
            });

            //新增/编辑收支分类保存之后回到主页面，重新加载主页面数据。
            abp.event.on('app.CreateOrEditIncomeExpendTypeModalSaved', function (result) {
                //alert('return value:' + result.classroomName);
                getIncomeExpendTypes(true);
            });

            //加载数据
            getIncomeExpendTypes();

            //光标定位到查询条件输入框
            $('#IETypesTableFilter').focus();
        });
       
        
       
    });
})();